import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;

import java.security.SecureRandom;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;
import java.util.Scanner;

/**
 * @version 1.0
 * @Author: 举个栗子吖
 * @Date: 2019-11-16 20:13
 */
public class AddSmsSign {


    private static final String SYMBOLS = "0123456789"; // 数字

    // 字符串
    // private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final Random RANDOM = new SecureRandom();

    /**
     * 获取长度为 6 的随机数字
     *
     * @return 随机数字
     */
    public static String getNonce_str() {

        // 如果需要4位，那 new char[4] 即可，其他位数同理可得
        char[] nonceChars = new char[6];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你的手机号");
        String phone = input.nextLine();
        String id = getNonce_str();
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4Fruy8fbq4wDL2CCcLt4", "bIqX5TqIKlmvxPgPMutEIogWdAhlBb");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
//        阿里api网址
        request.setDomain("dysmsapi.aliyuncs.com");
//      接口创建时间  不可变换时间
        request.setVersion("2017-05-25");
//        发送短信
        request.setAction("SendSms");
//        访问阿里Ip位置
        request.putQueryParameter("RegionId", "cn-hangzhou");
//        手机号
        request.putQueryParameter("PhoneNumbers", phone);
//        签名名称
        request.putQueryParameter("SignName", "举个栗子鸭");
//        模板CODE
        request.putQueryParameter("TemplateCode", "SMS_180049990");
//        发送随机验证码
        request.putQueryParameter("TemplateParam", "{code:'" + id + "'}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
